<template>
<div>
	<div class="g_top_box">
		<div class="g_right_box">
			<Button type="primary" @click="add">添加</Button>
		</div> 
		<div class="g_left_box"><h4>科目类型管理</h4></div>
	</div>
	
	<div class="g_list_box">
		<div class="g_list_box">
			<Table :columns="Columns" :data="list" style="width: 100%;"></Table>
		</div>
	</div>
	
	<Modal
		v-model="info.show"
		title="详细信息"
		@on-ok="info_ok"
		>
		<div>
			<Form :label-width="80">
				<FormItem label="名称">
					<Input v-model="info.info.name" style="width:220px;"></Input>
				</FormItem>
			</Form>
			
		</div>
	</Modal>
</div>
</template>

<script>
export default {
    name: 'bumen_list',
    data () {
		let self = this;
        return {
            Columns: [
				{
					title: 'ID',
					key: 'id',
					align: 'center',
                    width: 80
                },
				{
                    title: '名称',
					key: 'name',
					width: 200,
					render:(h,params)=>{
						if(params.row.is_edit){
							return h('Input',{
								props:{
									value:params.row.name
								},
								on:{
									'on-blur':(e)=>{
										self.list[params.index].name = e.target.value;
									}
								}
							},[
								h('Button',{
									props:{
										type: 'primary',
                                        size: 'small',
									},
									slot: 'append',
									on:{
										click:()=>{
											var postdata = {
												id:self.list[params.index].id,
												name:self.list[params.index].name,
											};
											
											Whttp.load('User/Subject/type_save',postdata,function(result){
												self.load_data();
											});
										}
									}
								},'提交')
							]);
						}
						else{
							return h('div',params.row.name);
						}
						
					}
                },
				{
                        title: '操作',
                        key: 'action',
                        render: (h, params) => {
                            return h('div', [
								
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small',
										icon:'edit'
                                    },
                                    style: {
                                        marginRight: '5px',
                                    },
                                    on: {
                                        click: () => {
											if(params.row.is_edit){
												self.list[params.index].is_edit = false;
											}
											else{
												self.list[params.index].is_edit = true;
											}
                                        }
                                    }
                                }, params.row.is_edit?'取消编辑':'编辑'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small',
										icon:'trash-a'
                                    },
									style: {
										display:params.row.username =='admin'?'none':'inline-block'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.row.id)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
            ],
            list: [],
			id:0,
			info:{
				show:false,
				info:{
					name:'',
				}
			},
			typelist:[]
        };
    },
	created(){
		this.id = this.$route.params.id;
		this.load_data();
	},
	methods:{
		load_data(){
			var self = this;
			var postdata = {};
			
			Whttp.load('User/Subject/type_get_list',postdata,function(result){
				for(var i=0;i<result.length;i++){
					result[i].is_edit = 0;
				}
				self.list = result;
			});
		},
		add(){
			this.info.info.name = '';
			this.info.show = true;
		},
		info_ok(){
			var self = this;
			var postdata = this.info.info;
			
			Whttp.load('User/Subject/type_save',postdata,function(result){
				self.load_data();
			});
		},
		remove(id){
			if(!confirm('是否删除')){
				return;
			}
			var self = this;
			var postdata = {
				id:id
			};
			Whttp.load('User/Subject/type_del',postdata,function(result){
				self.load_data();
			});
		},
	}
};
</script>